package edu.hunau.cxb.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 身份认证的拦截器
 * @author hb
 * @create 2020-04-08 11:29
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object users = session.getAttribute("users");
        if(users==null){
            //表示用户没有登录,跳转到登录页面
            request.setAttribute("errorMessage","需要先登录");
            request.getRequestDispatcher("/login.html").forward(request,response);

            return false;
        }
        return true;
    }
}
